package com.topcycle.tlight;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;

import com.mingle.entity.MenuEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DeviceManager {

	private static final String TAG = "DeviceManager";

	public static final int DEV_INIT_LIGHT_DEGREE = 100;
	public static final boolean DEV_INIT_IS_ON = true;

	private static ArrayList<Device> mDeviceList = new ArrayList<Device>();
	private static Device mCurrentDevice;

	public DeviceManager() {

	}
/*
	public static void createDeviceList(ArrayList<Integer> indexes) {
		if (indexes.size() < 0) {
			Log.e(TAG, "createDeviceList fail! indexs length < 0");
			return;
		}
		else if (indexes.size() == 0)
		{
			mDeviceList.clear();
			return;
		}
		// remove exist index in indexes
		Iterator<Device> iterator_1 = mDeviceList.iterator();
		while (iterator_1.hasNext()) {
			Device device_1 = iterator_1.next();
			if (indexes.contains((Integer) device_1.index)) {
				Log.i(TAG, "remove in indexex list: " + device_1.index);
				indexes.remove((Integer) device_1.index);
			} else {
				Log.i(TAG, "remove exist index in mDeviceList, index value: "
						+ device_1.index);
				iterator_1.remove();
			}
		}
		// add new device to mDeviceList
		for (int index : indexes) {
			addDevice(index);
		}
		mCurrentDevice = mDeviceList.get(0);
	}
*/
	public static Device getCurrentDevice() {
		return mCurrentDevice;
	}

	public static int getCurrentPos() {
		return mDeviceList.indexOf(mCurrentDevice);
	}

/*
	public static String getNameByIndex(int index) {
		Device device = getDeviceByMac(index);
		if (device.name == null) {
			return String.valueOf(device.index);
		}
		return device.name;
	}*/

//	public static List<String> getIndexStringList() {
//		List<String> indexList = new ArrayList<String>();
//		for (Device device : mDeviceList) {
//			if (device.name == null) {
//				indexList.add(String.valueOf(device.index));
//			} else {
//				indexList.add(device.name);
//			}
//		}
//		return indexList;
//	}

	public static List<MenuEntity> getIndexStringList() {
		List<MenuEntity> indexList = new ArrayList<MenuEntity>();
		MenuEntity menuEntity = new MenuEntity();
		menuEntity.iconId = R.drawable.ic_account_add;
		menuEntity.title = "Add";
		indexList.add(menuEntity);
		for (Device device : mDeviceList) {
			MenuEntity devEntity = new MenuEntity();
			devEntity.iconId = R.drawable.ic_account_child;
			if (device.name == null) {
				devEntity.title = "T-light_" +Utils.bytesToHexString(device.mac,3,3);
				indexList.add(devEntity);
			} else {
				devEntity.title = device.name;
				indexList.add(devEntity);
			}
		}
		return indexList;
	}

	public static Device updateCurrentDeviceByPosition(int pos) {
		if (mDeviceList.size() == 0) {
			mCurrentDevice = null;
			Log.i(TAG,"no device!");
			return null;
		}
		if (mDeviceList.size() < pos) {

			Log.i(TAG, "updateCurrentDeviceByPosition: position outof bound");
			return null;
		}

		mCurrentDevice = mDeviceList.get(pos);
		NetController.setInetAddress(mCurrentDevice.ip);
		return mCurrentDevice;
	}

	public static void resetTimer(byte[] mac) {
		Device device = getDeviceByMac(mac);
		if (device != null) {
			device.devClock.pIntent = null;
			device.devClock.isOn = false;
			device.devClock.year = 0;
			device.devClock.month = 0;
			device.devClock.day = 0;
			device.devClock.hour = 0;
			device.devClock.minute = 0;
		}
	}

	public static void updateCurrentDeviceState(boolean isOn, int light,
												int color) {
		if (mCurrentDevice == null) {
			return;
		}
		if (light >= 0) {
			mCurrentDevice.light = light;
		}
		mCurrentDevice.isOn = isOn;
		mCurrentDevice.color = color;
	}

	public static void printDeviceList() {
		for (Device dev : mDeviceList) {
			Log.i(TAG, "index: " + Utils.bytesToHexString(dev.mac,0,6) + ", light degree: " + dev.light
					+ ", on/off: " + dev.isOn);
		}
	}

	public static int getDeviceListSize() {
		return mDeviceList.size();
	}

	public static Device getDeviceByPos(int pos) {
		if (mDeviceList.size() < pos) {
			Log.i(TAG, "updateCurrentDeviceByPosition: position outof bound");
			return null;
		}
		return mDeviceList.get(pos);
	}

	public static Device getDeviceByMac(byte[] mac) {
		Device device = null;
		for (Device dev : mDeviceList) {
			if (Arrays.equals(mac,dev.mac)) {
				device = dev;
			}
		}
		return device;
	}

	public static void updateIpByMac(Context contexts,byte[] mac,byte[] ip) {
		Device device = getDeviceByMac(mac);
		if (device != null) {
			device.ip = ip;
			saveToLocal(contexts);
		}
	}

	public static void renameDevice(byte[] mac, byte[] ip, String name) {
		Device device = getDeviceByMac(mac);
		if (device == null) {
			device = addDevice(mac,ip);
		}
		if (name.isEmpty()) {

			device.name = "T-light_" + mac[3] + mac[4] + mac[5];
		} else {
			device.name = name;
		}
	}

	public static void delDevice() {
		mDeviceList.remove(mCurrentDevice);
	}

	public static void delDeviceByIndex(int index) {
		mDeviceList.remove(index);
	}

	private static Device addDevice(byte[] mac, byte[] ip) {
		int color = Color.argb(BitmapMatrix.PIC_ORI_ALPHA,
				BitmapMatrix.PIC_ORI_RED, BitmapMatrix.PIC_ORI_GREEN,
				BitmapMatrix.PIC_ORI_BLUE);
		Device device = new Device();
		device.mac = mac;
		device.ip = ip;
		device.light = DEV_INIT_LIGHT_DEGREE;
		device.isOn = DEV_INIT_IS_ON;
		device.color = color;
		device.name = null;
		device.devClock = new DeviceClock();
		mDeviceList.add(device);

		return device;
	}

	public static void saveToLocal(Context context) {
		try {
			Storage.saveData(mDeviceList, context);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void readFromLocal(Context context) {
		try {
			ArrayList<Device> tmplist = Storage.readData(context);
			if (tmplist != null) {
				mDeviceList = tmplist;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
